package com.springboot.config.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.SimpleHash;

/**
 * Created with IntelliJ IDEA.
 * User: lby.
 * Date: 2018/4/6.
 * Version: V1.0.
 */
public class CredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

        String tokenCredentials = String.valueOf(encrypt(String.valueOf(token.getPassword())));
        String accountCredentials = String.valueOf(getCredentials(info));
        //将密码加密与系统加密后的密码校验，内容一致就返回true,不一致就返回false
        return this.equals(tokenCredentials, accountCredentials);
    }

    //将传进来密码加密方法
    private Object encrypt(String data) {
        int hashIterations = 1024;//加密的次数
        Object salt = "zdog";//盐值
        String hashAlgorithmName = "MD5";//加密方式
        data = "123456";
        Object simpleHash = new SimpleHash(hashAlgorithmName, data, salt, hashIterations);
        return simpleHash;
    }
}
